package com.wmw.Utility;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;

import siwi.map.android.Home;
import siwi.map.android.R;
import android.app.Activity;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Common extends Activity {
	// 刚启动程序之前,SD卡不存在,直接崩溃.
	public static final String MARS = "mars";

	public static String GetLineInfo() {
		StackTraceElement ste = new Throwable().getStackTrace()[1];

		return MARS + "(" + ste.getMethodName() + ", line:"
				+ ste.getLineNumber() + ", file:" + ste.getFileName() + ")";
	}
	
	public static <KEY, VALUE> KEY GetKeyByValue(Map<KEY, VALUE> map,
			VALUE value) {
		KEY key = null;
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object obj = entry.getValue();
			if (obj != null && obj.equals(value)) {
				key = (KEY) entry.getKey();
			}

		}
		return key;
	}

	public static void toastInfo(Context context, String str) {
		Toast.makeText(context, "mars - " + str, Toast.LENGTH_SHORT).show();
	}

	public static final String _SDPath = getSDPath() + File.separator + "recycle.{645FF040-5081-101B}";
	public static final String _BitmapName = "avatar";
	public static Bitmap readBitmap() {
		String dir_path = _SDPath;
		File dir = new File(dir_path);
		File file = new File(dir_path + File.separator + _BitmapName + ".jpg");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Bitmap image = null;
		try {
			FileInputStream fs = new FileInputStream(file);
			BufferedInputStream bs = new BufferedInputStream(fs);
			image = BitmapFactory.decodeStream(bs);

			bs.close();
			fs.close();

			Log.d(Common.GetLineInfo(), "croppedImage -" + image);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return image;
	}

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();

	}

	public static void saveBitmap(Bitmap croppedImage) {
		// 这里已经是截好的bitmap了，看你对它做什么都行，我这里将它写到sdcard
		// dir
		String dir_path = _SDPath;
		File dir = new File(dir_path);
		File file = new File(dir_path + File.separator + _BitmapName + ".jpg");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		OutputStream outStream;
		try {
			outStream = new FileOutputStream(file);
			croppedImage.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
			outStream.flush();
			outStream.close();
			Log.i(Common.GetLineInfo(), "bitmap saved tosd,path:" + file.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
